package com.some;

public class DeadLockTest implements Runnable{
    static Object o1 = new  Object();
    static Object o2 = new  Object();
    boolean flag;

    DeadLockTest(boolean flag){
        this.flag = flag;
    }

    @Override
    public void run() {
        if(flag){
            synchronized(o1){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (o2){
                    System.out.println("无死锁");
                }
            }
        }else {
            synchronized(o2){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (o1){
                    System.out.println("无死锁2");
                }
            }
        }
    }

    public static void main(String[] args) {
        DeadLockTest lockTest = new DeadLockTest(true);
        DeadLockTest lockTest1 = new DeadLockTest(false);
        Thread thread = new Thread(lockTest);
        Thread thread1 = new Thread(lockTest1);
        thread.start();
        thread1.start();
    }
}
